package com.zyx.javademo.stream;

import java.util.Arrays;
import java.util.Random;
import java.util.function.ToIntFunction;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * @author Yaxi.Zhang
 * @since 2021/12/13 14:27
 * desc: 基本类型的Stream流
 */
public class StreamPrimaryDemo {
    public static void main(String[] args) {
        // range 包头不包尾, boxed将IntStream转为Stream<Integer>
        System.out.println(IntStream.range(0, 5).boxed().collect(Collectors.toList()));

        // mapToInt将Stream<Integer>转化为Intstream
        IntStream is = Stream.iterate(0, e -> e + 1).limit(10).mapToInt(e -> e);
        is.forEach(System.out::println);

        // toArray 将 IntStream 转 int[]
        Random r = new Random();
        int[] ints = IntStream.generate(() -> 65 + r.nextInt(10)).limit(20).toArray();
        System.out.println(new String(ints, 0, ints.length));

        // mapToObject
        Stream<String> strStream = IntStream.generate(() -> 65 + r.nextInt(10)).limit(20).mapToObj(i -> String.valueOf((char) i));
        System.out.println(strStream.collect(Collectors.joining()));



    }
}
